package com.platform.modular.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.common.model.Result;
import com.platform.common.utils.JwtUtils;
import com.platform.modular.feign.base.FinancialFeignService;
import com.platform.modular.feign.car.MerchantModelFeignService;
import com.platform.modular.feign.car.MerchantSeriesFeignService;
import com.platform.modular.feign.model.entity.MerchantAccountEntity;
import com.platform.modular.model.account.vo.MerchantBaseVO;
import com.platform.modular.model.base.FinancialSchemeDetail;
import com.platform.modular.model.car.dto.MerchantModelListDTO;
import com.platform.modular.model.car.vo.*;
import com.platform.modular.model.common.dto.CommonPageDTO;
import com.platform.modular.util.MiniprogramUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yifei.han
 * @version 1.0
 * @className NewCarController
 * @description 门店新车
 * @date 2022/3/14
 */
@Slf4j
@RestController
@RequestMapping(value = "/miniprogram/newcar")
public class NewCarController {

    @Autowired
    private MerchantSeriesFeignService merchantSeriesFeignService;

    @Autowired
    private MerchantModelFeignService merchantModelFeignService;

    @Autowired
    private MiniprogramUtil miniprogramUtil;

    @Autowired
    private FinancialFeignService financialFeignService;

    /**
     * 门店热门车系和车系列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/merchantSeriesWithHotSeries")
    public Result merchantSeriesWithHotSeries(HttpServletRequest request) {
        Long accountId = (Long)request.getAttribute("accountId");
        log.info("accountId:{}", accountId);
        Long merchantId = miniprogramUtil.obtainMerchantId(request);
        MerchantSeriesWithHotSeriesVO merchantSeriesPage = merchantSeriesFeignService.miniprogramMerchantSeriesWithHotSeries(merchantId);
        return Result.ok().setResult(merchantSeriesPage);
    }

    /**
     * 门店车系列表
     * @param request
     * @param pageDTO
     * @return
     */
    @RequestMapping(value = "/merchantSeriesList")
    public Result merchantSeriesList(HttpServletRequest request, @RequestBody CommonPageDTO pageDTO) {
        Long merchantId = miniprogramUtil.obtainMerchantId(request);
        Map<String, Object> param = new HashMap<>();
        param.put("pageNo", pageDTO.getPageNo());
        param.put("pageSize", pageDTO.getPageSize());
        param.put("merchantId", merchantId);
        IPage<MerchantSeriesVO> page = merchantSeriesFeignService.miniprogramMerchantSeriesPage(param);
        List<MerchantSeriesVO> merchantSeriesList = page.getRecords();
        return Result.ok().setResult(merchantSeriesList);
    }

    /**
     * 门店车系详情
     * @param request
     * @param merchantSeriesId
     * @return
     */
    @RequestMapping(value = "/merchantSeriesDetail")
    public Result merchantSeriesDetail(HttpServletRequest request, @RequestBody Long merchantSeriesId) {
        MerchantSeriesDetailVO merchantSeriesDetail = merchantSeriesFeignService.miniprogramMerchantSeriesDetail(merchantSeriesId);
        MerchantBaseVO merchantBaseVO = miniprogramUtil.obtainMerchantBase(request);
        merchantSeriesDetail.setMerchantBase(merchantBaseVO);
        return Result.ok().setResult(merchantSeriesDetail);
    }


    /**
     * 门店车型列表
     * @param merchantModelListDTO
     * @return
     */
    @RequestMapping(value = "/merchantModelList")
    public Result merchantModelList(@RequestBody MerchantModelListDTO merchantModelListDTO) {
        List<MerchantModelVO> merchantModelList = merchantModelFeignService.miniprogramMerchantModelList(merchantModelListDTO);
        return Result.ok().setResult(merchantModelList);
    }

    /**
     * 门店车型详情
     * @param merchantModelId
     * @return
     */
    @RequestMapping(value = "/merchantModelDetail")
    public Result merchantModelDetail(HttpServletRequest request, @RequestBody Long merchantModelId) {
        MerchantModelDetail merchantModelDetail = merchantModelFeignService.miniprogramMerchantModelDetail(merchantModelId);
        MerchantAccountEntity merchantAccount = miniprogramUtil.obtainExclusiveAdviser(request);
        merchantModelDetail.setAdviserMobile(merchantAccount.getMobile());
        MerchantBaseVO merchantBaseVO = miniprogramUtil.obtainMerchantBase(request);
        List<FinancialSchemeDetail> financialSchemeDetailList = financialFeignService.financialSchemeDetailList(merchantModelId);
        MerchantModelDetailVO merchantModelDetailVO = new MerchantModelDetailVO();
        merchantModelDetailVO.setMerchantModelDetail(merchantModelDetail);
        merchantModelDetailVO.setMerchantBaseVO(merchantBaseVO);
        merchantModelDetailVO.setFinancialSchemeDetailList(financialSchemeDetailList);
        return Result.ok().setResult(merchantModelDetailVO);
    }


    public static void main(String[] args) {
        String jwt = JwtUtils.createJWT("15720110083", 123L);
        System.out.println(jwt);
    }

}
